package cn.iocoder.yudao.module.famous.doctor.controller.admin.FamNotice;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.FamNotice.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.FamNotice.FamNoticeDO;
import cn.iocoder.yudao.module.famous.doctor.convert.FamNotice.FamNoticeConvert;
import cn.iocoder.yudao.module.famous.doctor.service.FamNotice.FamNoticeService;

@Tag(name = "管理后台 - 公告")
@RestController
@RequestMapping("/famous.doctor/fam-notice")
@Validated
public class FamNoticeController {

    @Resource
    private FamNoticeService famNoticeService;

    @PostMapping("/create")
    @Operation(summary = "创建公告")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-notice:create')")
    public CommonResult<Long> createFamNotice(@Valid @RequestBody FamNoticeCreateReqVO createReqVO) {
        return success(famNoticeService.createFamNotice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公告")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-notice:update')")
    public CommonResult<Boolean> updateFamNotice(@Valid @RequestBody FamNoticeUpdateReqVO updateReqVO) {
        famNoticeService.updateFamNotice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-notice:delete')")
    public CommonResult<Boolean> deleteFamNotice(@RequestParam("id") Long id) {
        famNoticeService.deleteFamNotice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-notice:query')")
    public CommonResult<FamNoticeRespVO> getFamNotice(@RequestParam("id") Long id) {
        FamNoticeDO famNotice = famNoticeService.getFamNotice(id);
        return success(FamNoticeConvert.INSTANCE.convert(famNotice));
    }

    @GetMapping("/list")
    @Operation(summary = "获得公告列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-notice:query')")
    public CommonResult<List<FamNoticeRespVO>> getFamNoticeList(@RequestParam("ids") Collection<Long> ids) {
        List<FamNoticeDO> list = famNoticeService.getFamNoticeList(ids);
        return success(FamNoticeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公告分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-notice:query')")
    public CommonResult<PageResult<FamNoticeRespVO>> getFamNoticePage(@Valid FamNoticePageReqVO pageVO) {
        PageResult<FamNoticeDO> pageResult = famNoticeService.getFamNoticePage(pageVO);
        return success(FamNoticeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公告 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-notice:export')")
    @OperateLog(type = EXPORT)
    public void exportFamNoticeExcel(@Valid FamNoticeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FamNoticeDO> list = famNoticeService.getFamNoticeList(exportReqVO);
        // 导出 Excel
        List<FamNoticeExcelVO> datas = FamNoticeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "公告.xls", "数据", FamNoticeExcelVO.class, datas);
    }

}
